package com.example.administrator.liveapp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.bean.LiveHomePageBean;
import com.example.administrator.liveapp.utils.AppUrl;

import java.util.List;

/**
 * Created by Administrator on 2017/5/24 0024.
 */

public class LiveFragmentAdapter2 extends RecyclerView.Adapter<LiveFragmentAdapter2.ViewHolder> implements View.OnClickListener {
    public Context context;
    public List<LiveHomePageBean.ActivityBean> activity;
    public LiveFragmentAdapter2(List<LiveHomePageBean.ActivityBean> activity){
        this.activity = activity;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View  view = LayoutInflater.from(parent.getContext()).inflate(R.layout.live_item,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        //将创建的View注册点击事件
        view.setOnClickListener(this);
        context = parent.getContext();
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.live_item_text.setText(activity.get(position).getActivitytitle());
        holder.live_Thumb_up_number.setText(activity.get(position).getActivityviewcount());
        holder.live_watch_number.setText(activity.get(position).getActivitybestcount());
        String imageUrl = activity.get(position).getActivityimgurl();
        if(activity.get(position).getActivityimgurl()!=null&&!activity.get(position).getActivityimgurl().equals("")){
            imageUrl=imageUrl.substring(1,imageUrl.indexOf("|"));
        }
        Glide.with(context)
                .load(AppUrl.URL+imageUrl)
                .into(holder.live_item_ac_preview);
//        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return activity.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView live_item_text;
        public RadioButton live_Thumb_up_number;
        public RadioButton live_watch_number;
        public ImageView live_item_ac_preview;
        public ViewHolder(View itemView) {
            super(itemView);
            live_item_text = (TextView) itemView.findViewById(R.id.live_item_text);
            live_Thumb_up_number = (RadioButton) itemView.findViewById(R.id.live_Thumb_up_number);
            live_watch_number = (RadioButton) itemView.findViewById(R.id.live_watch_number);
            live_item_ac_preview = (ImageView) itemView.findViewById(R.id.live_item_ac_preview);
        }
    }


    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }
    private OnItemClickListener mOnItemClickListener = null;

    //define interface
    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
